package com.hsh.gaoqianlianmeng.service.impl;

import com.hsh.gaoqianlianmeng.comparator.OrderComparator;
import com.hsh.gaoqianlianmeng.domain.Order;
import com.hsh.gaoqianlianmeng.service.OrderService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    /**
     * 合并订单，
     * 当订单里的 收件人 姓名，地址，手机号 一致时
     * 两个订单合并（商品信息合并）
     *
     * @param orders
     * @return
     */
    @Override
    public <T extends Order> List<T> mergeOrder(List<T> orders) {
        Map<String, T> orderMap = orders.stream().collect(Collectors.toMap(T::getCustomerInfo, o -> o
                , (existing, replacement) -> {
                    existing.setProductInfo(existing.getProductInfo() + "，" + replacement.getProductInfo())
                            .setNo(existing.getNo() + "，" + replacement.getNo());
                    return existing;
                }));
        orders = orderMap.values().stream().sorted(OrderComparator::compare).collect(Collectors.toList());
        return orders;
    }
}
